package com.warmlight.voicepacket.ui.imlm;

import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v7.app.AlertDialog;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.warmlight.voicepacket.R;
import com.warmlight.voicepacket.data.eventbus.Login;
import com.warmlight.voicepacket.ui.user.usercenter.LookUserCenterActivity;
import com.warmlight.voicepacket.utils.ImConnect;
import com.warmlight.voicepacket.utils.SPUtils;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import io.rong.imkit.RongIM;
import io.rong.imlib.RongIMClient;
import io.rong.imlib.model.Conversation;
import io.rong.imlib.model.Message;
import io.rong.imlib.model.UserInfo;

/**
 * 聊天页面
 */
public class ConversationActivity extends FragmentActivity {
    private LinearLayout llBack;
    private TextView tvHeaderTitlename;
    private LinearLayout llHeaderRight;
    private TextView tvSelectAll;
    private String type="拉黑";
    private ImageView ivHeaderRight;
    private String id;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_conversation);

      id= getIntent().getData().getQueryParameter("targetId");
      //每次进来刷新缓存  Todo

      ImConnect.getInstance().SetUserById(id);

      String nickname=  getIntent().getData().getQueryParameter("title");

//      RongContext.getInstance().getUserInfoFromCache(id).getName();

        llBack = (LinearLayout) findViewById(R.id.ll_back);
        tvHeaderTitlename = (TextView) findViewById(R.id.tv_header_titlename);
        if (!TextUtils.isEmpty(nickname)){
            tvHeaderTitlename.setText(nickname);
        }
        llHeaderRight = (LinearLayout) findViewById(R.id.ll_header_right);
        tvSelectAll = (TextView) findViewById(R.id.tv_select_all);
        tvSelectAll.setVisibility(View.VISIBLE);

        RongIM.getInstance().getBlacklistStatus(id, new RongIMClient.ResultCallback<RongIMClient.BlacklistStatus>() {//获取状态
            @Override
            public void onSuccess(RongIMClient.BlacklistStatus blacklistStatus) {
                    blacklistStatus.getValue();
                if (blacklistStatus.getValue()==1){
                    tvSelectAll.setText("拉黑");
                    type="拉黑";
                }else {
                    tvSelectAll.setText("取消拉黑");
                    type="取消拉黑";
                }
            }

            @Override
            public void onError(RongIMClient.ErrorCode errorCode) {

            }
        });
        RongIM.getInstance().setConversationClickListener(new RongIM.ConversationClickListener() {
            @Override
            public boolean onUserPortraitClick(Context context, Conversation.ConversationType conversationType, UserInfo userInfo, String s) {
                Intent intent = new Intent(context, LookUserCenterActivity.class);
                intent.putExtra("uid", s);
                context.startActivity(intent);
                return true;
            }

            @Override
            public boolean onUserPortraitLongClick(Context context, Conversation.ConversationType conversationType, UserInfo userInfo, String s) {
                return false;
            }

            @Override
            public boolean onMessageClick(Context context, View view, Message message) {
                return false;
            }

            @Override
            public boolean onMessageLinkClick(Context context, String s, Message message) {
                return false;
            }

            @Override
            public boolean onMessageLongClick(Context context, View view, Message message) {
                return false;
            }
        });
        ivHeaderRight = (ImageView) findViewById(R.id.iv_header_right);
        llBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });

        llHeaderRight.setOnClickListener(new View.OnClickListener() {//
            @Override
            public void onClick(View view) {
                if (type.equals("拉黑")){
                    new  AlertDialog.Builder(ConversationActivity.this)
                            .setTitle("确认拉黑该用户？")
                            .setMessage("拉黑后将不在收到该用户的聊天信息")
                            .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialog, int which) {
                                }
                            })
                            .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialog, int which) {
                                    RongIM.getInstance().addToBlacklist(id, new RongIMClient.OperationCallback() {
                                        @Override
                                        public void onSuccess() {
                                            tvSelectAll.setText("取消拉黑");
                                            type="取消拉黑";
                                        }

                                        @Override
                                        public void onError(RongIMClient.ErrorCode errorCode) {

                                        }
                                    });

                                }
                            })
                            .create().show();
                }else {
                    new  AlertDialog.Builder(ConversationActivity.this)
                            .setTitle("确认取消拉黑该用户？")
                            .setMessage("取消拉黑后将收到该用户的聊天信息")
                            .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialog, int which) {
                                }
                            })
                            .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialog, int which) {
                                    RongIM.getInstance().removeFromBlacklist(id, new RongIMClient.OperationCallback() {
                                        @Override
                                        public void onSuccess() {
                                            tvSelectAll.setText("拉黑");
                                            type="拉黑";
                                        }

                                        @Override
                                        public void onError(RongIMClient.ErrorCode errorCode) {

                                        }
                                    });

                                }
                            })
                            .create().show();
                }

            }
        });

        EventBus.getDefault().register(this);
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void XXX(Login login){
        if (!SPUtils.isLogined()){
            finish();
        }
    }
}
